Allow specifying profiles to `cargo rustc`
authorAlex Crichton <alex@alexcrichton.com>
Thu, 17 Dec 2015 18:10:59 +0000 (10:10 -0800)
committerAlex Crichton <alex@alexcrichton.com>
Thu, 17 Dec 2015 18:12:41 +0000 (10:12 -0800)
This should allow compiling the specified target in the various profiles that
are available to it, e.g. bench or test in addition to the standard
dev.

Closes #2120

src/bin/rustc.rs
tests/test_cargo_rustc.rs

index 14f9e120b655f360f030d5c8f7d8f92b70aaa9eb..391e5f41f5fda53471869d020598c36fe8c974dd 100644 (file)
@@ -1,9 +1,9 @@
 use std::env;
 
-use cargo::ops::CompileOptions;
+use cargo::ops::{CompileOptions, CompileMode};
 use cargo::ops;
 use cargo::util::important_paths::{find_root_manifest_for_wd};
-use cargo::util::{CliResult, Config};
+use cargo::util::{CliResult, CliError, Config};
 
 #[derive(RustcDecodable)]
 struct Options {
@@ -23,6 +23,7 @@ struct Options {
     flag_example: Vec<String>,
     flag_test: Vec<String>,
     flag_bench: Vec<String>,
+    flag_profile: Option<String>,
 }
 
 pub const USAGE: &'static str = "
@@ -41,6 +42,7 @@ Options:
     --test NAME              Build only the specified test target
     --bench NAME             Build only the specified benchmark target
     --release                Build artifacts in release mode, with optimizations
+    --profile PROFILE        Profile to build the selected target for
     --features FEATURES      Features to compile for the package
     --no-default-features    Do not compile default features for the package
     --target TRIPLE          Target triple which compiles will be for
@@ -69,6 +71,15 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
 
     let root = try!(find_root_manifest_for_wd(options.flag_manifest_path,
                                               config.cwd()));
+    let mode = match options.flag_profile.as_ref().map(|t| &t[..]) {
+        Some("dev") | None => CompileMode::Build,
+        Some("test") => CompileMode::Test,
+        Some("bench") => CompileMode::Bench,
+        Some(mode) => {
+            return Err(CliError::new(&format!("unknown profile: `{}`, use dev,
+                                               test, or bench", mode), 101))
+        }
+    };
 
     let opts = CompileOptions {
         config: config,
@@ -78,7 +89,7 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
         no_default_features: options.flag_no_default_features,
         spec: &options.flag_package.map_or(Vec::new(), |s| vec![s]),
         exec_engine: None,
-        mode: ops::CompileMode::Build,
+        mode: mode,
         release: options.flag_release,
         filter: ops::CompileFilter::new(options.flag_lib,
                                         &options.flag_bin,
index 559a5f722ccc11da066d34cac0466dd7c80470b3..d79ccd9b6bd1730736b0c99b26a5a90318dc268f 100644 (file)
@@ -1,8 +1,9 @@
 use std::path::MAIN_SEPARATOR as SEP;
+
 use support::{execs, project};
 use support::{COMPILING, RUNNING};
-use hamcrest::{assert_that};
 
+use hamcrest::{assert_that, existing_file};
 
 fn setup() {
 }
@@ -353,3 +354,33 @@ Invalid arguments.
 Usage:
     cargo rustc [options] [--] [<opts>...]".to_string()));
 });
+
+test!(rustc_with_other_profile {
+    let foo = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "foo"
+            version = "0.0.1"
+            authors = []
+
+            [dev-dependencies]
+            a = { path = "a" }
+        "#)
+        .file("src/main.rs", r#"
+            #[cfg(test)] extern crate a;
+
+            #[test]
+            fn foo() {}
+        "#)
+        .file("a/Cargo.toml", r#"
+            [package]
+            name = "a"
+            version = "0.1.0"
+            authors = []
+        "#)
+        .file("a/src/lib.rs", "");
+    foo.build();
+
+    assert_that(foo.cargo("rustc").arg("--profile").arg("test"),
+                execs().with_status(0));
+});